package com.andkrup.easygame.game {
	import com.andkrup.easygame.game.objects.ICharacterGroup;	
	
	import flash.display.DisplayObject;	
	
	import com.andkrup.easygame.game.objects.ICharacterFactory;	
	import com.andkrup.easygame.game.objects.ICharacter;	
	import com.andkrup.easygame.config.GameConfiguration;	
	import com.andkrup.easygame.data.SaveGame;	
	import com.andkrup.easygame.game.commands.IGameCommand;	
	import com.andkrup.easygame.ui.IViewPort;	
	import com.andkrup.easygame.events.GameEvent;	
	
	import flash.events.EventDispatcher;	
	import flash.events.Event;	
	
	/**
	 * @author Andkrup
	 * 
	 * A Game object is defined by having a player with a Career and a list of
	 * Missions to start.
	 * 
	 * The Game is happening from whenever the player is modifying something the
	 * game should remember to when teh player stops playing. Starting a new
	 * mission is not starting a new Game
	 */
	internal class Game extends EventDispatcher {
		private var base:IViewPort;
		private var interval:uint;
		private var tickaway:Boolean;
		private var currentMission:IMission;
		private var charGroup:ICharacterGroup;

		public function get viewport():IViewPort{
			return base;
		}
		public function set viewport(value:IViewPort):void{
			if(base){
				base.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
			}
			base = value;
			if(base){
				base.addEventListener(Event.ENTER_FRAME, onEnterFrame, false, 0, true);
			}
		}
		
		public function get running():Boolean{
			return tickaway;
		}
		
		public function get characterGroup():ICharacterGroup{
			return charGroup;
		}
		public function set characterGroup(value:ICharacterGroup):void{
			charGroup = value;
		}

		public function Game(cfg:GameConfiguration = null){
			var clock:Boolean = false;
			var interval:uint = 20;
			
			if(cfg){
				trace("Game:> ctr() load config keys: "+cfg.getKeys());
			}
			
			if(clock){
				this.interval = interval;
				tickaway = false;
			}
		}
		
		public function start():void{
			tickaway = true;
			dispatchEvent(new GameEvent(GameEvent.START));
		}
		public function stop():void{
			tickaway = false;
			dispatchEvent(new GameEvent(GameEvent.STOP));
		}
		public function abort():void{
			tickaway = false;
			dispatchEvent(new GameEvent(GameEvent.ABORT));
		}
		public function pause():void{
			tickaway = !tickaway;
			dispatchEvent(new GameEvent(tickaway ? GameEvent.UNPAUSE : GameEvent.PAUSE));
		}
		
		public function startMission(mission:IMission):void{
			if(currentMission){
				return;
			}
			
			currentMission = mission;
			
			if(viewport){
				viewport.addMap(currentMission.map);
			}
			var charFactory:ICharacterFactory = EasyGameEngine.instance.currentGamePackage.characterFactory;
			var arr:Array = mission.playerCharacters;
			var sprite:DisplayObject;
			for each(var char:ICharacter in arr){
				sprite = charFactory.createSprite(char);
				viewport.addCharacterSprite(sprite);
			}
			
			if(!running){
				
			}
		}
		public function abortMission(mission:IMission):void{
			
		}
		/**
		 * Give this instance a gamecommand (user-instruction).
		 * 
		 * Return true if the command can be digested by this class
		 */
		public function digestCommand(command:IGameCommand):Boolean{
			return false;
		}
		
		/**
		 * Give up gamedata to a writer object
		 */
		public function writeSaveGame(saveGame:SaveGame):void{
		}
		
		/**
		 * Primary Game-loop
		 */
		private function onEnterFrame(e: Event) : void {
			if(tickaway){
				dispatchEvent(new GameEvent(GameEvent.TICK));
			}
		}
	}
}
